using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Playables;

namespace Battle.Graphics
{
    public class ActionBaseNode
    {
        string actionName;
        YooAsset.AssetHandle actionHandle;
        AnimationClip animationClip;
        Animator _animator;
        PlayableGraph _graph;
        AnimationClipPlayable _clipPlayable;
        AnimationPlayableOutput _playableOutput;

        public ActionBaseNode(Animator animator,string actionName)
        {
            _animator = animator;
            this.actionName = actionName;
        }

        public void Stop()
        {
            _graph.Stop();
        }

        public void Start()
        {
            _graph.Play();
        }

        public void OnCreate()
        {
            actionHandle = YooAsset.YooAssets.LoadAssetSync<AnimationClip>(actionName);
            animationClip = (AnimationClip)actionHandle.AssetObject;
            _graph = PlayableGraph.Create(actionName);
            _graph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
            _clipPlayable = AnimationClipPlayable.Create(_graph, animationClip);
            _playableOutput = AnimationPlayableOutput.Create(_graph, "Idle", _animator);
            _playableOutput.SetSourcePlayable(_clipPlayable);
        }

        public void OnDestroy()
        {
            _graph.Destroy();
            animationClip = null;
            actionHandle.Dispose();
            actionHandle = null;
        }
    }
}
